package cn.edu.jssvc.litai.phonetest238;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.IBinder;
import android.os.PowerManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class MyService extends Service {
    TelephonyManager telephonyManager;//定义全局变量

    SensorManager sensorManager;
    Sensor proximitySensor;

    PowerManager powerManager;
    PowerManager.WakeLock wakeLock;

    @SuppressLint("InvalidWakeLockTag")
    @Override
    public void onCreate() {//初始化活动
        super.onCreate();
        telephonyManager=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);//记得去androidManifest申明权限
        sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
        proximitySensor=sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

        powerManager=(PowerManager)getSystemService(POWER_SERVICE);
        wakeLock=powerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK,"mywakelock");
    }

    public MyService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        telephonyManager.listen(new MyPhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE);
        return super.onStartCommand(intent, flags, startId);
    }

    class MyPhoneStateListener extends PhoneStateListener implements SensorEventListener {

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }

        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType()==Sensor.TYPE_PROXIMITY){
                float distance=event.values[0];
                if (distance==0){
                    if (wakeLock.isHeld()==false){
                        wakeLock.acquire();
                    Log.d("tag-sensor", "锁屏");
                    }
                }else {
                    if (wakeLock.isHeld()){
                        wakeLock.release();

                    Log.d("tag-sensor", "亮屏");
                    }
                }
            }
        }

        @Override
        public void onCallStateChanged(int state, String phoneNumber) {
            super.onCallStateChanged(state, phoneNumber);
            switch (state){
                case TelephonyManager.CALL_STATE_IDLE:
                    Log.d("tag-liatener","来电去电挂断");
                    if (sensorManager!=null){
                        sensorManager.unregisterListener(this);
                        Log.d("tag-sensor","取消注册传感器");
                    }
                    break;
                case  TelephonyManager.CALL_STATE_RINGING:
                    Log.d("tag-liatener","来电响铃等待接通"+phoneNumber);
                    break;
                case  TelephonyManager.CALL_STATE_OFFHOOK:
                    Log.d("tag-liatener","来电去电接通");
                    if (sensorManager!=null){
                        sensorManager.registerListener(this,proximitySensor,SensorManager.SENSOR_DELAY_NORMAL);
                        Log.d("tag-sensor","注册传感器");
                    }
                    break;
                default:
                    break;
            }
        }
    }
}